<?php

/**
 * 汽车控制器
 *
 *
 * @package  app
 * @extends  Controller
 */
namespace web;

class Controller_Admin_Car extends \Controller_FilterController {

    public $template = 'admin/default/template';

    public function before(){
        parent::before();
    }

    /**
    * 车型列表
    * 
    **/
    public function action_index(){
        $params = array(
            'title' => '车型管理',
            'menu' => 'member-index'
        );

        $ids = \Model_Category::getChildIds(\Session::get('seller')->car_category);
        $items = \Model_Car::query()
                ->where('category_id', 'IN', $ids);

        //分页查询
        $count = $items->count();
        $config = array(
            'pagination_url' => "/web/admin/article",
            'total_items'    => $count,
            'per_page'       => \Input::get('count', 10),
            'uri_segment'    => 'start',
            'show_first'     => true,
            'show_last'      => true,
            'name'           => 'bootstrap3_cn'
        );
        
        $pagination = new \Pagination($config);
        $params['pagination'] = $pagination;        
        $params['items'] = $items
                            ->rows_offset($pagination->offset)
                            ->rows_limit($pagination->per_page)
                            ->get();

        \View::set_global($params);
        $this->template->content = \View::forge('admin/default/car/index');
    }

    /**
    * 创建或编辑车型信息
    * 
    * 当车型ID不为0时的操作为编辑车型。反之为创建
    * @param $id 车型ID
    * 
    **/
    public function action_save($id = 0){
        $params = array(
            'title' => '车型详情',
            'menu' => 'car-details'
        );

        if($id){
            $car = \Model_Car::find($id);
        }

        if(\Input::method() == 'POST'){
            if(! \Security::check_token()){
                die('过期或无效的请求');
            }
            $data = \Input::post();
            if(isset($car) && $car){
                $car->set($data);
            }else{
                $car = \Model_Car::forge($data);
            }
            
            if($car->save()){
                \Session::set_flash('msg', array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0)); 
            }else{
                \Session::set_flash('msg', array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20)); 
            }
        }

        $cat = \Model_Category::find(\Session::get('seller')->car_category);
        $params['cats'] = $cat->children()->get();
        if( ! $params['cats']){
            $params['cats'] = array($cat);
        }

        if(isset($car) && $car){
            $params['item'] = \Model_Car::find($id);
        }

        \View::set_global($params);
        $this->template->content = \View::forge('admin/default/car/details');
    }

    /**
    * 车型系列列表
    **/
    public function action_series(){
        $params = array(
            'title' => '资讯管理',
            'menu' => 'member-index'
        );

        $items = \Model_Car::query();
        $items->where('seller_id', \Session::get('seller')->id);

        //分页查询
        $count = $items->count();
        $config = array(
            'pagination_url' => "/web/admin/article",
            'total_items'    => $count,
            'per_page'       => \Input::get('count', 10),
            'uri_segment'    => 'start',
            'show_first'     => true,
            'show_last'      => true,
            'name'           => 'bootstrap3_cn'
        );
        
        $pagination = new \Pagination($config);
        $params['pagination'] = $pagination;        
        $params['items'] = $items
                            ->rows_offset($pagination->offset)
                            ->rows_limit($pagination->per_page)
                            ->get();
        $params['levels'] = \Model_MemberLevel::query()->get();

        \View::set_global($params);
        $this->template->content = \View::forge('admin/default/car/series');
    }

    /**
    * 系列创建或编辑
    * 
    * 当系列ID不为0时的操作为编辑系列。反之为创建
    * @param $id 系列ID
    * 
    **/
    public function action_series_save($id = 0){
        
    }

    /**
    * 车型系列列表
    **/
    public function action_features(){
        
    }

    /**
    * 创建或编辑车型特色
    * 
    * 当系列ID不为0时的操作为编辑系列。反之为创建
    * @param $id 特色ID
    * 
    **/
    public function action_feature_save($id = 0){

    }

    /**
    * 车型图片列表
    **/
    public function action_galleries(){
        
    }

    /**
    * 创建或编辑车型
    * 
    * 当图片ID不为0时的操作为编辑系列。反之为创建
    * @param $id 图片ID
    * 
    **/
    public function action_gallery_save($id = 0){

    }
}